﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.Mime;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using GurLoader.Preferences;
using GurLoader.Util;
using GurLoader.View;

namespace GurLoader.ViewModel
{
    public class NotifyTrayViewModel
    {
        private Preferences.GurLoaderPreferences AppPreferences { get; set; }
        private IApplication CurrentApplication { get; set; }
        private ICommandManager CommandManager { get; set; }

        private readonly object _preferencesWindowLock = new object();
        private bool IsPreferencesWindowShow { get; set; }


        public NotifyTrayViewModel(Preferences.GurLoaderPreferences appPreferences, IApplication currentApplication, ICommandManager commandManager)
        {
            AppPreferences = appPreferences;
            CurrentApplication = currentApplication;
            CommandManager = commandManager;
        }

        public ICommand ShowPreferencesCommand
        {
            get
            {
                return new DelegateCommand
                {
                    CommandAction = () =>
                    {
                        PreferencesWindow window = new PreferencesWindow();
                        PreferencesViewModel pvm = new PreferencesViewModel( AppPreferences, CommandManager);
                        window.DataContext = pvm;

                        pvm.RequestClose += (sender, args) => window.Close();
                        window.Closing += (sender, args) => IsPreferencesWindowShow = false;

                        lock( _preferencesWindowLock )
                            IsPreferencesWindowShow = true;

                        window.Show();
                    },
                    CanExecuteFunc = () =>
                    {
                        lock (_preferencesWindowLock)
                            return !IsPreferencesWindowShow;
                    }
                };
            }
        }

        public ICommand ExitApplicationCommand
        {
            get
            {
                return new DelegateCommand
                {
                    CommandAction = () => CurrentApplication.Shutdown()
                };
            }
        }
    }
}
